import {
  SAVEUSERCHANNELS,
  SETALLCHANNELS,
  SETARTICLELIST,
  SETMOREACTION,
} from '../action_types/home'

const initvalue = {
  Userchannels: [],
  allchannels: [],
  articles: {},
  moreaction: {
    dispatch: false,
    articleId: '',
    channelId: '',
  },
}
export const home = (state = initvalue, action) => {
  const { type, payload } = action
  if (type === SAVEUSERCHANNELS) {
    return {
      ...state,
      Userchannels: payload,
    }
  }
  if (type === SETALLCHANNELS) {
    return {
      ...state,
      allchannels: payload,
    }
  }
  if (type === SETARTICLELIST) {
    const { channelId, timestamp, list, loa } = payload
    console.log(payload)
    return {
      ...state,
      articles: {
        ...state.articles,
        [channelId]: {
          timestamp: timestamp,
          list: loa ? [...state.articles[channelId].list, ...list] : list,
        },
      },
    }
  }
  if (type === SETMOREACTION) {
    return {
      ...state,
      moreaction: payload,
    }
  }
  return state
}
